﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.IO;
using System.Text;
using System.Windows.Forms;
using PlaylistEditorForCentrafuse.Playlist;

namespace PlaylistEditorForCentrafuse.GUI
{
    public partial class frmEntryDetails : Form
    {
        public PlaylistEntry PlaylistEntry { get; set; }

        public frmEntryDetails()
        {
            InitializeComponent();
        }

        private void frmEntryDetails_Shown(object sender, EventArgs e)
        {
            // Daten anzeigen
            txtFilename.Text = PlaylistEntry.Filename;
            txtTitle.Text = PlaylistEntry.Title;
            txtInterpret.Text = PlaylistEntry.Interpret;
            txtAlbum.Text = PlaylistEntry.Album;
            txtGenre.Text = PlaylistEntry.Genre;
            txtSongnr.Text = PlaylistEntry.SongNr;
            txtYear.Text = PlaylistEntry.Year;
            txtDate1.Text = PlaylistEntry.Date1;
            txtDate2.Text = PlaylistEntry.Date2;
            txtEmpty1.Text = PlaylistEntry.Empty1;
            txtEmpty2.Text = PlaylistEntry.Empty2;

            btnQuery.Enabled = PlaylistEntry.Audiofile != null;
        }

        private void frmEntryDetails_FormClosing(object sender, FormClosingEventArgs e)
        {
            // Speichern ?
            if (txtTitle.Text != PlaylistEntry.Title ||
                txtInterpret.Text != PlaylistEntry.Interpret ||
                txtAlbum.Text != PlaylistEntry.Album ||
                txtGenre.Text != PlaylistEntry.Genre ||
                txtSongnr.Text != PlaylistEntry.SongNr ||
                txtYear.Text != PlaylistEntry.Year ||
                txtDate1.Text != PlaylistEntry.Date1 ||
                txtDate2.Text != PlaylistEntry.Date2 ||
                txtEmpty1.Text != PlaylistEntry.Empty1 ||
                txtEmpty2.Text != PlaylistEntry.Empty2)
            {
                DialogResult res = MessageBox.Show("Die Daten wurden verändert.\nÄnderungen übernehmen ?", 
                    "Änderungen speichern", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
                
                // Speichern
                if (res == DialogResult.Yes)
                {
                    PlaylistEntry.Title = txtTitle.Text;
                    PlaylistEntry.Interpret = txtInterpret.Text;
                    PlaylistEntry.Album = txtAlbum.Text;
                    PlaylistEntry.Genre = txtGenre.Text;
                    PlaylistEntry.SongNr = txtSongnr.Text;
                    PlaylistEntry.Year = txtYear.Text;
                    PlaylistEntry.Date1 = txtDate1.Text;
                    PlaylistEntry.Date2 = txtDate2.Text;
                    PlaylistEntry.Empty1 = txtEmpty1.Text;
                    PlaylistEntry.Empty2 = txtEmpty2.Text;
                }
                // Nicht schliessen
                else if (res == DialogResult.Cancel)
                {
                    e.Cancel = true;
                }
            }// if (txtShowname.Text != currentProps.title)
        }

        private void btnQuery_ButtonClick(object sender, EventArgs e)
        {
            if (PlaylistEntry.Audiofile.ID3v2 != null)
            {
                GetDataFromID3();
            }
            else
            {
                GetDataFromFilename();
            }
        }

        private void GetDataFromID3()
        {
            txtDate1.Text = DateTime.Now.ToString();
            txtDate2.Text = DateTime.Now.ToString();
            txtEmpty1.Text = "0";
            txtEmpty2.Text = "0";
            
            if (PlaylistEntry.Audiofile.ID3v2 != null)
            {
                // CF3 Style
                txtTitle.Text = PlaylistEntry.Audiofile.ID3v2.Title;
                txtInterpret.Text = PlaylistEntry.Audiofile.ID3v2.Artist;
                txtAlbum.Text = PlaylistEntry.Audiofile.ID3v2.Album;
                txtGenre.Text = PlaylistEntry.Audiofile.ID3v2.Genre;
                txtSongnr.Text = PlaylistEntry.Audiofile.ID3v2.Track;
                txtYear.Text = PlaylistEntry.Audiofile.ID3v2.Year;
            }
            else
            {
                MessageBox.Show("Keine ID3v2 Informationen verfügbar", "Daten abrufen",
                    MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void GetDataFromFilename()
        {
            txtDate1.Text = DateTime.Now.ToString();
            txtDate2.Text = DateTime.Now.ToString();
            txtEmpty1.Text = "0";
            txtEmpty2.Text = "0";

            txtTitle.Text = PlaylistEntry.Audiofile.GetTitleFromFilename();
            txtInterpret.Text = PlaylistEntry.Audiofile.GetArtistFromFilename();
        }

        private void toolMenuID3_Click(object sender, EventArgs e)
        {
            GetDataFromID3();
        }

        private void toolMenuFilename_Click(object sender, EventArgs e)
        {
            GetDataFromFilename();
        }
    }
}
